import jinja2
import webapp2
import json
import os
from google.appengine.api import users

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class Widget(webapp2.RequestHandler):
	viewPath = "view/" 	
		
	def tpl(self, tpl, values):
		if os.path.exists(tpl):
			template = jinja_environment.get_template(tpl)
			return template.render(values)
		return ""
	
	def get(self):
		user = users.get_current_user()		
		values = {'user': user}
		args = self.request.arguments()
				
		for name in args:
			values[name] = self.request.get(name)
		
		script = self.viewPath + self.request.get('view')
		
		ret = {
				'html' : self.tpl(script +'.html', values),
				'js' : self.tpl(script +'.js', values),
				'css' : self.tpl(script +'.css', values)
				}					
		
		out = json.dumps(ret, separators=(',', ': '))
		self.response.out.write(out)
